package com.example.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.example.constants.ErrorCodeEnum;
import com.example.vo.res.ErrorJsonResult;

/**
 * 全局异常统一处理类
 * 
 * @author lli
 *
 * @version 1.0
 */
@ControllerAdvice(basePackages = "com.example.controller")
public class GlobalExceptionHandler {
    public final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(BusinessErrorException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorJsonResult handleBusinessError(BusinessErrorException ex) {
        return new ErrorJsonResult(ex);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorJsonResult handleUnexpectedServer(RuntimeException ex) {
        return new ErrorJsonResult(ErrorCodeEnum.EXCEPTION, ex.getMessage());
    }

}
